package com.github.chain.admin.api.controller;

import com.github.chain.admin.client.UserServiceClient;
import com.github.chain.admin.common.domain.User;
import com.github.chain.admin.common.group.InserterGroup;
import com.github.chain.admin.common.group.UpdateGroup;
import com.github.chain.admin.common.group.UserPasswordGroup;
import com.github.chain.admin.common.request.UserRequest;
import com.github.framework.core.Result;
import com.github.framework.core.page.DataPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author AIQ
 * @date 2023/5/11
 * @project er-trader
 */

@Controller
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MainController.class);

    @Resource
    private UserServiceClient userServiceClient;

    @GetMapping("/main/system/user/index")
    @RequiresPermissions("system:user:index") //权限访问
    public String index(Model model, UserRequest userRequest){

        Result<DataPage<User>> result = userServiceClient.findUserByPage(userRequest);
        DataPage<User> dataPage = new DataPage<>();
        List<User> userList = new ArrayList<>();
        if (result.isSuccess()){
            dataPage = result.getData();
            userList = dataPage.getDataList();
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("list", userList);
        return "/manager/user/index";
    }

    @GetMapping("/system/user/detail/{id}")
    @RequiresPermissions("system:user:detail")
    public String detail(@PathVariable("id") Long id, Model model){
        Result<User> result = userServiceClient.findUserById(id);
        if (result.isSuccess()){
            model.addAttribute("user",result.getData());
        }
        return "/manager/user/detail";
    }

    @GetMapping("/editPwd")
    public String toEditPassword() {
        return "/manager/user/pwd";
    }

    @GetMapping("/system/user/add")
    @RequiresPermissions("system:user:add")
    public String add(){
        return "/manager/user/add";
    }

    @PostMapping("/system/user/save")
    @RequiresPermissions("system:user:add")
    public @ResponseBody Result save(@Validated(value = InserterGroup.class) UserRequest userRequest){
        return userServiceClient.saveUser(userRequest);
    }

    @GetMapping("/system/user/edit/{id}")
    @RequiresPermissions("system:user:edit")
    public String toEdit(@PathVariable Long id, Model model){
        Result<User> result = userServiceClient.findUserById(id);
        if (result.isSuccess()){
            model.addAttribute("user", result.getData());
        }
        return "/manager/user/edit";
    }

    @PostMapping("/system/user/edit")
    @RequiresPermissions("system:user:edit")
    @ResponseBody
    public Result<Integer> update(@Validated(value = UpdateGroup.class) UserRequest userRequest){
        return userServiceClient.updateUser(userRequest);
    }

    @GetMapping("/system/user/delete/{id}")
    @RequiresPermissions("system:user:delete")
    @ResponseBody
    public Result delete(@PathVariable Long id) {
        return userServiceClient.deleteUserById(id);
    }

    @PostMapping("/system/user/status/start")
    @RequiresPermissions("system:user:status")
    @ResponseBody
    public Result enableStatus(@RequestParam List<Long> ids){
        return userServiceClient.enableUserStatus(ids);
    }

    @PostMapping("/system/user/status/stop")
    @RequiresPermissions("system:user:status")
    @ResponseBody
    public Result disEnableStatus(@RequestParam List<Long> ids){
        return userServiceClient.disableUserStatus(ids);
    }

    @GetMapping ("/system/user/pwd")
    @RequiresPermissions("system:user:pwd")
    public String toModifyPwd(@RequestParam("ids") Long id,  Model model){

        model.addAttribute("id", id);
        return "/manager/user/pwd";
    }

    @PostMapping("/system/user/pwd")
    @RequiresPermissions("system:user:pwd")
    @ResponseBody
    public Result<Integer> modifyUserPassword(@Validated(value = {UserPasswordGroup.class}) UserRequest userRequest){

        return userServiceClient.modifyUserPassword(userRequest);
    }

    @GetMapping("/system/user/role")
    @RequiresPermissions("system:user:role")
    public String auth(@RequestParam("ids") Long id, Model model){

        Result<User> result = userServiceClient.roleAssignmentById(id);

        if (result.isSuccess()){
            model.addAttribute("list", result.getData().getList());
            model.addAttribute("authRoles", result.getData().getAuthSet());
            model.addAttribute("id", result.getData().getId());
        }

        return "/manager/user/role";
    }

    @PostMapping("/system/user/role")
    @RequiresPermissions("system:user:role")
    @ResponseBody
    public Result auth(@RequestParam("id") Long id, @RequestParam(value = "roleId",required = false) List<Long> roleId){
        UserRequest userRequest = new UserRequest();
        userRequest.setId(id);
        userRequest.setRoleIds(roleId);
        return userServiceClient.authUserRole(userRequest);
    }
}
